// This File Replicates the Analysis in 
//"Effects of Credit Supply on Stock Market Booms and Busts"
// by Hansman, Hong, Jiang, Liu, and Meng
// Created: 09/25/2024 

// -----------------------------------------------------------------------------
// Set-Up
// -----------------------------------------------------------------------------
clear all
/*
// installing required packages
ssc install estout
ssc install rdrobust
ssc install ranktest
ssc install rddensity
ssc install lpdensity
ssc install egenmore

* Install ftools (remove program if it existed previously)
cap ado uninstall ftools
net install ftools, from("https://raw.githubusercontent.com/sergiocorreia/ftools/master/src/")

* Install reghdfe
cap ado uninstall reghdfe
net install reghdfe, from("https://raw.githubusercontent.com/sergiocorreia/reghdfe/master/src/")

* Install ivreg2, the core package
cap ado uninstall ivreg2
ssc install ivreg2

* Finally, install this package
cap ado uninstall ivreghdfe
net install ivreghdfe, from("https://raw.githubusercontent.com/sergiocorreia/ivreghdfe/master/src/")
*/

// Set Scheme
set scheme stcolor

// Set Seed0
set seed 123

// Change to the location of the "do" folder
cd "/HHJLM_Replication_RFS/do"	

// -----------------------------------------------------------------------------
// Cleaning
// -----------------------------------------------------------------------------
	// Basic Cleaning
	// Daily Stock Data
	do 01_generate_stock_daily.do // Daily Stock Data With DGTW Returns

	// Monthly Stock Data
	do 01_generate_stock_monthly_vintage // Monthly Stock Data With Vintage Assignments
	
	// Other Auxilliary Data
	//Overall Institutional Ownership Data
	do 01_generate_io_stacked
	//Mutual Fund Holding Data
	do 01_generate_mf_hold
	// Rolling Stock Ranking
	do 01_generate_ranking_rolling
	
	//Screening and Ranking
	do 01_generate_daily_tot.do // Criteria For Screening and Ranking	
	do 01_generate_last_10_12_2012.do  // Screening Rules Vintage 1
	do 01_generate_last_6_8_2013.do // Screening Rules Vintage 2
	do 01_generate_last_6_8_2014.do // Screening Rules Vintage 3
	
	// Simple Vintage Assignment Crosswalk
	do 01_code_vintage_crosswalk // Vintages by Stock Code
	
	// Preparing Data for Analysis
	do 01_generate_event_study_data // Prepare Data for Event Study
	do 01_generate_rd_index // Compute Inclusion Index
	do 01_generate_daily_returns_rd // Prepare Data for RD
	do 01_generate_model_estimation_data // Prepare Data for Model

// -----------------------------------------------------------------------------
// Analysis
// -----------------------------------------------------------------------------
	do 02_rollout_summary // (Table 1)
	do 02_event_study // Event Study (Table 2 - Panel A)
	do 02_regression_discontinuity // Regression Discontinuity (Table 3 - All Panels)
	do 02_mf_portfolio_weights // Portfolio Weights (Table 4)
	do 02_tbm_returns // To-Be-Marginable Returns (Table 5)
	do 02_estimate_model // Information Revelation model (Table 6)
	do 02_margin_debt_bust // Margin Debt and The Bust (Table 7)
	
// -----------------------------------------------------------------------------
// Plots
// -----------------------------------------------------------------------------	
	do 03_plot_aggregate_margin_cap // Figure 1
	do 03_plot_vintage_margin_cap //Figure 2
	do 03_plot_density_test //Figure 3
	do 03_plot_rd // Figure 4
	do 03_plot_institutional_ownership // Figure 5
	do 03_plot_simulations // Figure 6 - panels a + b
	do 03_plot_model_fit //Figure 6 - panel c
